package com.mingqijia.gassafety.shared.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class RedisCacheExt  {
    @Autowired
    private RedisTemplate redisTemplate;

    public Object getHashObj(String key, String hashKey) {
        return redisTemplate.opsForHash().get(key,hashKey);
    }

    public Boolean setIfAbsent(String key, Object value, Long timeout) {
        Boolean result = true;

        try {
            if (timeout == null) {
                result = this.redisTemplate.opsForValue().setIfAbsent(key, value);
            } else {
                result = this.redisTemplate.opsForValue().setIfAbsent(key, value, timeout, TimeUnit.SECONDS);
            }
        } catch (Exception var6) {
            log.error("redis加锁失败");
        }

        return result;
    }

    public void del(String... key) {
        try {
            if (key != null && key.length > 0) {
                if (key.length == 1) {
                    this.redisTemplate.delete(key[0]);
                } else {
                    this.redisTemplate.delete(CollectionUtils.arrayToList(key));
                }
            }
        } catch (Exception var3) {
            log.error("删除redis key失败，key" + key);
        }
    }

    public void hashDel(String key,String hashKey) {
        try {
            this.redisTemplate.opsForHash().delete(key,hashKey);
        } catch (Exception var3) {
            log.error("删除redis hash key失败，key" + key);
        }
    }
}
